﻿//===============================================================================
// SharePoint911 - http://www.SharePoint911.com
// SharePoint ActiveX Override (SPAXO)
// Created By: Larry J. Riemann
//===============================================================================
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
// IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
// POSSIBILITY OF SUCH DAMAGE.
//===============================================================================

using SharePoint911.SPAXO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using System.Text;

namespace TestCases
{
    
    
    /// <summary>
    ///This is a test class for InsertContentFilterStreamTest and is intended
    ///to contain all InsertContentFilterStreamTest Unit Tests
    ///</summary>
    [TestClass()]
    public class InsertContentFilterStreamTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        /// <summary>
        ///A test for normal Write
        ///</summary>
        [TestMethod()]
        public void WriteNormalTest()
        {
            using (Stream baseStream = new MemoryStream())
            {
                Encoding encode = Encoding.UTF8;
                string searchTerm = "</HEAD>";
                string dataToInsert = "MyInsertedString";
                InsertContentFilterStream target = new InsertContentFilterStream(baseStream, encode, searchTerm, dataToInsert);

                byte[] buffer = encode.GetBytes("<HTML><HEAD></HEAD></HTML>");
                int offset = 0;
                int count = buffer.Length;
                target.Write(buffer, offset, count);
                target.Flush();

                byte[] resultArray = new byte[(int)baseStream.Length];
                baseStream.Position = 0;
                baseStream.Read(resultArray, 0, resultArray.Length);
                string result = encode.GetString(resultArray);

                Assert.AreEqual("<HTML><HEAD>MyInsertedString</HEAD></HTML>", result);
            }
        }

        /// <summary>
        ///A test for multiple normal Writes
        ///</summary>
        [TestMethod()]
        public void WriteNormalMultipleWriteTest()
        {
            using (Stream baseStream = new MemoryStream())
            {
                Encoding encode = Encoding.UTF8;
                string searchTerm = "</HEAD>";
                string dataToInsert = "MyInsertedString";
                InsertContentFilterStream target = new InsertContentFilterStream(baseStream, encode, searchTerm, dataToInsert);

                byte[] buffer = encode.GetBytes("<HTML><HEAD></HEAD></HTML>");
                int offset = 0;
                int count = buffer.Length;
                target.Write(buffer, offset, 25);
                target.Write(buffer, 25, count - 25);
                target.Flush();

                byte[] resultArray = new byte[(int)baseStream.Length];
                baseStream.Position = 0;
                baseStream.Read(resultArray, 0, resultArray.Length);
                string result = encode.GetString(resultArray);

                Assert.AreEqual("<HTML><HEAD>MyInsertedString</HEAD></HTML>", result);
            }
        }

        /// <summary>
        ///A test for multiple normal Writes
        ///</summary>
        [TestMethod()]
        public void WriteNormalMultipleWriteSmallBufferTest()
        {
            using (Stream baseStream = new MemoryStream())
            {
                Encoding encode = Encoding.UTF8;
                string searchTerm = "</HEAD>";
                string dataToInsert = "MyInsertedString";
                InsertContentFilterStream target = new InsertContentFilterStream(baseStream, encode, searchTerm, dataToInsert);

                byte[] buffer = encode.GetBytes("<HTML><HEAD></HEAD></HTML>");
                int offset = 0;
                int count = buffer.Length;
                target.Write(buffer, offset, 5);
                target.Write(buffer, 5, count - 5);
                target.Flush();

                byte[] resultArray = new byte[(int)baseStream.Length];
                baseStream.Position = 0;
                baseStream.Read(resultArray, 0, resultArray.Length);
                string result = encode.GetString(resultArray);

                Assert.AreEqual("<HTML><HEAD>MyInsertedString</HEAD></HTML>", result);
            }
        }

        /// <summary>
        ///A test for cross buffered search and write
        ///</summary>
        [TestMethod()]
        public void WriteInsertSplitTermAcrossWriteTest()
        {
            using (Stream baseStream = new MemoryStream())
            {
                Encoding encode = Encoding.UTF8;
                string searchTerm = "</HEAD>";
                string dataToInsert = "MyInsertedString";
                InsertContentFilterStream target = new InsertContentFilterStream(baseStream, encode, searchTerm, dataToInsert);

                byte[] buffer = encode.GetBytes("<HTML><HEAD></HEAD></HTML>");
                int offset = 0;
                int count = buffer.Length;
                target.Write(buffer, offset, 15);
                target.Write(buffer, 15, count - 15);
                target.Flush();

                byte[] resultArray = new byte[(int)baseStream.Length];
                baseStream.Position = 0;
                baseStream.Read(resultArray, 0, resultArray.Length);
                string result = encode.GetString(resultArray);

                Assert.AreEqual("<HTML><HEAD>MyInsertedString</HEAD></HTML>", result);
            }
        }    
    }
}
